package helpdesk;

import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author MattLampe
 */
public class GraduateAssistantGUI extends javax.swing.JFrame
{
    ArrayList<Announcement> announcementData;
    User loggedInGradA = null;
    /**
     * Creates new form GraduateAssistantGUI
     */
    public GraduateAssistantGUI()
    {
        initComponents();
        setVisible(true);
    }
    
    public GraduateAssistantGUI(User gradA)
    {
        //gets announcement data and fills the table by using a for loop
        announcementData = MongoDB.getRecentAnnouncements();
        loggedInGradA = gradA;
        
        initComponents();
        
        System.out.println(announcementTable);
        announcementTable.getColumnModel().getColumn(0).setHeaderValue("Author");
        announcementTable.getColumnModel().getColumn(1).setHeaderValue("Title");
        announcementTable.getColumnModel().getColumn(2).setHeaderValue("Body");
        
        for(int i = 0; i < announcementData.size(); i++)
        {
            announcementTable.setValueAt(announcementData.get(i).author, i, 0);
            announcementTable.setValueAt(announcementData.get(i).title, i, 1);
            announcementTable.setValueAt(announcementData.get(i).body, i, 2);
        }
        
        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        graduateAssistantLoginTitle = new javax.swing.JLabel();
        graduateAssistantPostAnnouncementButton = new javax.swing.JButton();
        graduateAssistantViewStudentQueriesButton = new javax.swing.JButton();
        graduateAssistantViewDocumentsButton = new javax.swing.JButton();
        graduateAssistantSubmitScheduleButton = new javax.swing.JButton();
        graduateAssistantExitButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        announcementTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        graduateAssistantLoginTitle.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        graduateAssistantLoginTitle.setText("Graduate Assistant Page");

        graduateAssistantPostAnnouncementButton.setText("Post Announcement");
        graduateAssistantPostAnnouncementButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateAssistantPostAnnouncementButtonActionPerformed(evt);
            }
        });

        graduateAssistantViewStudentQueriesButton.setText("View Queries");
        graduateAssistantViewStudentQueriesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateAssistantViewStudentQueriesButtonActionPerformed(evt);
            }
        });

        graduateAssistantViewDocumentsButton.setText("View Uploaded Documents");
        graduateAssistantViewDocumentsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateAssistantViewDocumentsButtonActionPerformed(evt);
            }
        });

        graduateAssistantSubmitScheduleButton.setText("Submit Availability Schedule");
        graduateAssistantSubmitScheduleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateAssistantSubmitScheduleButtonActionPerformed(evt);
            }
        });

        graduateAssistantExitButton.setText("Exit");
        graduateAssistantExitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graduateAssistantExitButtonActionPerformed(evt);
            }
        });

        announcementTable.setModel(new AbstractTableModel()
            {
                @Override
                public int getRowCount()
                {
                    return announcementData.size();
                }

                @Override
                public int getColumnCount()
                {
                    return 3;
                }

                @Override
                public String getValueAt(int i, int j)
                {
                    String temp = null;
                    switch (j)
                    {
                        case 0:
                        temp = announcementData.get(i).author;
                        break;
                        case 1:
                        temp = announcementData.get(i).title;
                        break;
                        case 2:
                        temp = announcementData.get(i).body;
                        break;
                    }
                    return temp;
                }

                public void setValueAt(String val, int i, int j)
                {
                    switch (j)
                    {
                        case 1:
                        announcementData.get(i).author = val;
                        break;
                        case 2:
                        announcementData.get(i).title = val;
                        break;
                        case 3:
                        announcementData.get(i).body = val;
                        break;
                    }
                }
            });
            jScrollPane1.setViewportView(announcementTable);

            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(graduateAssistantExitButton)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 574, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(layout.createSequentialGroup()
                                    .add(graduateAssistantPostAnnouncementButton)
                                    .add(18, 18, 18)
                                    .add(graduateAssistantViewStudentQueriesButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                    .add(graduateAssistantSubmitScheduleButton)
                                    .add(18, 18, 18)
                                    .add(graduateAssistantViewDocumentsButton))))
                        .add(layout.createSequentialGroup()
                            .add(202, 202, 202)
                            .add(graduateAssistantLoginTitle)))
                    .addContainerGap(25, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(20, 20, 20)
                    .add(graduateAssistantLoginTitle)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(graduateAssistantPostAnnouncementButton)
                        .add(graduateAssistantViewStudentQueriesButton)
                        .add(graduateAssistantViewDocumentsButton)
                        .add(graduateAssistantSubmitScheduleButton))
                    .add(39, 39, 39)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 235, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 35, Short.MAX_VALUE)
                    .add(graduateAssistantExitButton)
                    .add(29, 29, 29))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents
    //This button exits the GraduateAssistantGUI and opens a login form
    private void graduateAssistantExitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateAssistantExitButtonActionPerformed
        new Login();//.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_graduateAssistantExitButtonActionPerformed
    //This button opens a page to add a new announcement
    private void graduateAssistantPostAnnouncementButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_graduateAssistantPostAnnouncementButtonActionPerformed
    {//GEN-HEADEREND:event_graduateAssistantPostAnnouncementButtonActionPerformed
        AddAnouncement announce = new AddAnouncement(loggedInGradA);
        this.dispose();
    }//GEN-LAST:event_graduateAssistantPostAnnouncementButtonActionPerformed
    //This button views the forum with student queries
    private void graduateAssistantViewStudentQueriesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateAssistantViewStudentQueriesButtonActionPerformed
        ArrayList<Post> forumPosts = MongoDB.getRecentPosts(loggedInGradA);

        ForumGUI table = new ForumGUI(forumPosts, loggedInGradA);
        
        table.setVisible(true);
    }//GEN-LAST:event_graduateAssistantViewStudentQueriesButtonActionPerformed
    //This button opens a page to view uploaded documents
    private void graduateAssistantViewDocumentsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateAssistantViewDocumentsButtonActionPerformed
        new ViewDocGUI(GFS.retrieveAllDocs("forms", MongoDB.getConnection()), "forms");
    }//GEN-LAST:event_graduateAssistantViewDocumentsButtonActionPerformed
    //This button opens a directory viewer to upload a pdf version of a schedule
    private void graduateAssistantSubmitScheduleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateAssistantSubmitScheduleButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setAcceptAllFileFilterUsed(false);
        ExtensionFileFilter filter1 = new ExtensionFileFilter("PDF", new String[] { "PDF", "pdf"});
        chooser.setFileFilter(filter1);
        chooser.showDialog(this, "Open");
        File fileObj = chooser.getSelectedFile();
        GFS.saveFile(fileObj, loggedInGradA.username, "sched", MongoDB.getConnection()); 
    }//GEN-LAST:event_graduateAssistantSubmitScheduleButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GraduateAssistantGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GraduateAssistantGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GraduateAssistantGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GraduateAssistantGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GraduateAssistantGUI();//.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable announcementTable;
    private javax.swing.JButton graduateAssistantExitButton;
    private javax.swing.JLabel graduateAssistantLoginTitle;
    private javax.swing.JButton graduateAssistantPostAnnouncementButton;
    private javax.swing.JButton graduateAssistantSubmitScheduleButton;
    private javax.swing.JButton graduateAssistantViewDocumentsButton;
    private javax.swing.JButton graduateAssistantViewStudentQueriesButton;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}